package com.system.boot.setting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.api.SuperInterface;
import com.system.api.setting.entity.dto.DictFormDto;
import com.system.api.setting.entity.po.SysIcon;
import com.system.api.setting.entity.search.DictSearch;
import com.system.boot.setting.converter.DictConverter;
import com.system.api.setting.entity.po.SysDict;
import com.system.boot.setting.service.DictService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.result.BaseController;
import org.yang.common.core.result.R;
import org.yang.database.cache.utils.RedisHsahUtils;
import org.yang.database.orm.entity.BaseEnabled;
import org.yang.database.orm.entity.BaseQuery;

import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * 字典目录
 *
 * @author: lslands
 * @since: 1.0
 * @create: 2024/5/18
 * @description:
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
public class DictController extends BaseController {

    private final DictService dictService;
    private final DictConverter dictConverter;
    private final SuperInterface<SysDict> superInterface;

    /**
     * 新增/修改
     * @param dto 新增对象
     * @apiNote 通过是否存在主键,判定是新增还是修改;id为空则为新增，反之;
     * @author lslands
     * @date 2024/5/18 23:06
     * @return R<void>
     */
    @PostMapping("/saveOrUpdate")
    public R save(@RequestBody @Valid DictFormDto dto) throws BaseException {
        SysDict record = dictConverter.formEntity(dto);
        return Objects.isNull(dto.getId()) ?toCreateAjax(superInterface.handInsert(record))
                :toUpdateAjax(superInterface.handUpdateById(record));
    }

    /**
     * 更新状态
     * @param record 更新对象
     * @return R<void>
     */
    @PatchMapping("/updateStatus")
    public R updateStatus(@RequestBody BaseEnabled record) throws BaseException {
        return toUpdateAjax(superInterface
                .handEnabledById(record.getId(),record.getEnabled(),record.getRevision()));
    }

    /**
     * 单条数据查询
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 23:05
     * @return SysDict
     */
    @GetMapping("/getById")
    public R<SysDict> getById(@RequestParam Long id){
        return R.success(dictService.getById(id));
    }

    /**
     * 分页模糊查询列表
     * @param search 模糊查询对象
     * @return R<Page<SysDict>>
     */
    @GetMapping("/page")
    public R<Page<SysDict>> page(@ModelAttribute DictSearch search) throws BaseException {
        BaseQuery query = dictConverter.queryEntity(search);
        SysDict sysDict = dictConverter.queryPoEntity(search);
        return R.success(superInterface.getPage(query,sysDict));
    }


    /**
     * 单条逻辑删除
     * @apiNote 该删除存在关联操作,删除字典目录，同时也会删除对应的词条
     * @param id 主键ID|1
     * @author lslands
     * @date 2024/5/19 22:28
     */
    @DeleteMapping("/deleteOne")
    public R deleteOne(@RequestBody Long id) throws BaseException {
        return toDeleteAjax(superInterface.deleteOne(id));
    }

    /**
     * 批量删除
     * @apiNote 该删除存在关联操作,删除字典目录，同时也会删除对应的词条
     * @param ids 主键ID集|[1,2]
     * @param delete 是否物理删除标记|false
     * @author lslands
     * @date 2024/5/19 22:29
     */
    @DeleteMapping("/deleteBatch/{delete}")
    public R deleteBatch(@RequestBody List<Long> ids,@PathVariable("delete") Boolean delete) throws BaseException {
        if (ids.isEmpty()){
            return toDeleteAjax(false);
        }
        return toDeleteAjax(superInterface.deleteBatch(delete,ids));
    }



}
